﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Buccontrols;
using PluginInterface;
using Buccaneer;
using System.IO;

namespace NotepadPlugin
{
    public class OpenAction: PluginAction
    {
        public OpenAction(IPlugin plugin)
        {
            base.name = "open";
            base.func = new ActionFunction(this.OpenFile);
            base.pars.Add("path", "the path of the file to open");
            base.exceptions.Add("System.IO.IOException", new ActionFunction(ReadWriteError));
            base.plugin = plugin;
        }

        public void OpenFile(string[] args)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.DefaultExt = "*.txt";
            ofd.Filter = "Tekstbestanden|*.txt|Alle bestanden|*.*";
            ofd.RestoreDirectory = true;
            ofd.Title = "Open een tekstbestand";
            ofd.ValidateNames = true;
            ofd.ShowDialog(this.plugin.MainInterface);
            StreamReader sr = new StreamReader(ofd.FileName);
            (base.plugin.MainInterface as Interface).LoadText(sr.ReadToEnd());
        }

        public void ReadWriteError(string[] args)
        {
            Options.WriteComment("Er ging iets mis bij het openen van het bestand.");
        }
    }
}
